package com.hlkj.warboot.configurer.webmvc;

import com.hlkj.warboot.configurer.annotation.LoginUserInfoMethodArgumentResolver;
import com.hlkj.warboot.configurer.properties.WxProperties;
import com.hlkj.warboot.module_wx.configurer.Intercept.DrawLimitIntercept;
import com.hlkj.warboot.module_wx.configurer.Intercept.AuthIntercept;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: TODO webMvc配置
 * @Author: 李星 lixing_java@163.com
 * @Date: 2020/2/19 22:23
 */
@Component
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /*
     * @Description TODO 视图控制器
     * @Author 李星
     * @Date 2020/2/19 22:26
     * @Param [registry]
     * @return void
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index"); // 项目入口
        registry.addViewController("/403").setViewName("403"); // 后台访问403页面
        registry.addViewController("/404").setViewName("404"); // 后台访问404页面
    }

    /*
     * @Description TODO  静态资源解析器
     * @Author 李星
     * @Date 2020/2/19 22:27
     * @Param [registry]
     * @return void
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将所有/static/** 访问都映射到classpath:/static/目录下
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    /*
     * @Description TODO  拦截器
     * @Author 李星
     * @Date 2020/2/19 22:27
     * @Param [registry]
     * @return void
     */
    @Resource
    private WxProperties wxProperties;
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 系统模块拦截器
        //registry.addInterceptor(new SysIntercept()).addPathPatterns("/**").excludePathPatterns("/","/sys/admin/login","/static/**","/index.html");
        // 接口防刷拦截器
        // registry.addInterceptor(new AccessLimitIntercept(redisTemplate)).addPathPatterns("/**").excludePathPatterns("/","/sys/admin/login","/static/**","/index.html");
        // 微信活动.用户进入首页静默授权拦截器
        registry.addInterceptor(new AuthIntercept(wxProperties)).addPathPatterns("/wx/hkSeckill/index/**");
        // 微信活动.号卡秒杀拦截器
        registry.addInterceptor(new DrawLimitIntercept(redisTemplate)).addPathPatterns("/wx/hkSeckill/seckill/**");
    }

    /*
     * @Description 跨域
     * @Author 李星
     * @Date 2020/2/19 22:28
     * @Param [registry]
     * @return void
     */
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins("*")
//                .allowedHeaders("*")
//                .allowedMethods("*")
//                .allowCredentials(true)
//                .maxAge(1800);
//    }

    /*
     * @Description 参数解析器
     * @Author 李星
     * @Date 2020/2/20 13:34
     * @Param [resolvers]
     * @return void
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        // 登录用户对象
        resolvers.add(new LoginUserInfoMethodArgumentResolver());
    }

}
